package com.example.iotmanagerment26.controller;

import com.example.iotmanagerment26.domain.Device;
import com.example.iotmanagerment26.domain.DeviceWithLocation;
import com.example.iotmanagerment26.domain.JsonData;
import com.example.iotmanagerment26.domain.DeviceLocation;
import com.example.iotmanagerment26.service.DeviceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/v1/device")
@Tag(name="设备管理",description = "设备管理的接口")
public class DeviceController {

    @Autowired
    DeviceService deviceService;

    @GetMapping("/selectDeviceById")
    @Operation(summary = "通过设备id查询设备信息")
    public JsonData selectDeviceById(int id) {
        Device result = deviceService.selectDeviceById(id);
        if (result!=null) {
            return new JsonData(1,result,"查询成功");
        }else {
            return new JsonData(0,result,"查询失败");
        }
    }

    @GetMapping("/selectDeviceByName")
    @Operation(summary = "通过设备名字查询设备信息")
    public JsonData selectDeviceByName(String name) {
        Device result = deviceService.selectDeviceByName(name);
        if (result!=null) {
            return new JsonData(1,result,"查询成功");
        }else {
            return new JsonData(0,result,"查询失败");
        }
    }

    @GetMapping("/selectAllDevice")
    @Operation(summary = "查询所有设备")
    public JsonData selectAllDevice() {
        List<DeviceWithLocation> results=deviceService.selectAllDevice();
        if (results!=null) {
            return new JsonData(1,results,"查询成功");
        }else {
            return new JsonData(0,results,"查询失败");
        }
    }

    @GetMapping("/selectAllDeviceLocation")
    @Operation(summary = "查询所有有标记坐标的设备")
    public JsonData selectAllDeviceLocation() {
        List<DeviceLocation> results=deviceService.selectAllDeviceLocation();
        if (results!=null) {
            return new JsonData(1,results,"查询成功");
        }else {
            return new JsonData(0,results,"查询失败");
        }
    }

    @PostMapping("/insertDevice")
    @Operation(summary = "增加新设备")
    public JsonData insertDevice(@RequestBody Device device){
        int result=deviceService.insertDevice(device);
        if (result>=1){
            return new JsonData(1,device,"插入成功");
        }else {
            return new JsonData(0,result,"插入失败");
        }
    }

    @GetMapping("/deleteDeviceById")
    @Operation(summary = "通过设备id删除设备")
    public JsonData deleteDeviceById(int id){
        int result=deviceService.deleteDeviceById(id);
        if (result>=1){
            return new JsonData(1,id,"删除成功");
        }else {
            return new JsonData(0,result,"删除失败");
        }
    }

    @PostMapping("/updateInfoById")
    @Operation(summary = "修改设备信息")
    public JsonData updateInfoById(@RequestBody Device device){
        int result=deviceService.updateInfoById(device);
        if (result>=1){
            return new JsonData(1,device.getId(),"修改成功");
        }else {
            return new JsonData(0,device.getId(),"修改失败");
        }
    }
}
